//
//  MeAddressViewController.m
//  ChinaA
//
//  Created by Mxionlly on 2018/10/30.
//  Copyright © 2018 Evan. All rights reserved.
//

#import "MeAddressViewController.h"
#import "UINavigationItem+Item.h"
#import "AddressManagementCell.h"
#import <SDAutoLayout/SDAutoLayout.h>
#import "taskUserManagerService.h"
#import "EmptyView.h"

@interface MeAddressViewController ()<UITableViewDelegate,UITableViewDataSource>
@property (nonatomic,strong)UITableView       *tableView;
@property (nonatomic,strong)NSArray           *dataArray;
@property (nonatomic,strong)EmptyView         *emptyView;
@end

@implementation MeAddressViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    self.title = @"收货地址";
    // Do any additional setup after loading the view.
    [self.navigationItem setItem:@"新增地址" image:nil highlightImage:nil target:self selector:@selector(rightButtonAction) color:[UIColor whiteColor] left:NO enabled:YES];
    [self.view addSubview:self.tableView];
}

-(void)viewWillAppear:(BOOL)animated
{
    [super viewWillAppear:animated];
    [self loadData];
}

-(void)loadData{
    self.dataArray = [NSArray new];
    [taskUserManagerService getReceivingAddressList:nil success:^(dataAddressListModel *response, BOOL success) {
        if (success) {
            self.dataArray = response.data;
            if (self.dataArray.count > 0) {
                [self.tableView reloadData];
                [self.emptyView removeFromSuperview];
            }else{
                [self.view addSubview:self.emptyView];
            }
        }else{
            [DKProgressHUD showErrorWithStatus:response.message];
        }
    } failture:^(NSError *error, NSString *strDescription) {
        [DKProgressHUD dismiss];
        [DKProgressHUD showErrorWithStatus:strDescription];
    }];
}

-(void)rightButtonAction{
    [self.navigationController pushViewController:[MeNewAddressViewController new] animated:YES];
}

#pragma mark - init
-(EmptyView *)emptyView
{
    if (!_emptyView) {
        _emptyView = [EmptyView getEmptyView];
        _emptyView.frame = CGRectMake(0, distanceHeight, KScreenWidth, KScreenHeight-distanceHeight);
    }
    return _emptyView;
}

-(UITableView *)tableView
{
    if (!_tableView) {
        _tableView = [[UITableView alloc]initWithFrame:CGRectMake(0, distanceHeight, KScreenWidth, KScreenHeight-distanceHeight) style:UITableViewStylePlain];
        _tableView.dataSource = self;
        _tableView.delegate = self;
        _tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
        _tableView.backgroundColor  = [UIColor whiteColor];
        _tableView.scrollEnabled = YES;
        _tableView.estimatedSectionFooterHeight = 0;
        _tableView.estimatedSectionHeaderHeight = 0;
        [_tableView registerClass:[AddressManagementCell class] forCellReuseIdentifier:@"AddressManagementCell"];
    }
    return _tableView;
}

-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return self.dataArray.count;
}

-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    dataAddressModel *info = [self.dataArray objectAtIndex:indexPath.row];
    id model = [dataAddressModel mj_objectWithKeyValues:info];
    return [self.tableView cellHeightForIndexPath:indexPath model:model keyPath:@"model" cellClass:[AddressManagementCell class] contentViewWidth:KScreenWidth];
}

-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    AddressManagementCell *cell = [tableView dequeueReusableCellWithIdentifier:@"AddressManagementCell"];
    cell.editButton.tag = indexPath.row;
    [cell.editButton addTarget:self action:@selector(editAction:) forControlEvents:UIControlEventTouchUpInside];
    if (self.dataArray.count > 0) {
        dataAddressModel *info = [self.dataArray objectAtIndex:indexPath.row];
        dataAddressModel *infoModel = [dataAddressModel mj_objectWithKeyValues:info];
        [cell setModel:infoModel];
    }
    return cell;
}


-(void)editAction:(UIButton *)sender{
    if (self.dataArray.count > 0) {
        dataAddressModel *info = [self.dataArray objectAtIndex:sender.tag];
        dataAddressModel *infoModel = [dataAddressModel mj_objectWithKeyValues:info];
        MeNewAddressViewController *detail = [MeNewAddressViewController new];
        detail.isModify = 1;
        detail.model =infoModel;
        [self.navigationController pushViewController:detail animated:YES];
    }
}


/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
